package com.xhis.homenurse.webservice.api;

import com.xhis.homenurse.webservice.convert.GsonConverterFactory;
import com.xhis.homenurse.webservice.convert.MyXmlConverterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * @author panghuan
 * @Description : Retrofit请求配置
 * create at 18/3/20 下午2:06
 */
public class ApiConfig
{
    private static Retrofit mRetrofit;
    /**
     * @return 获得Retrofit基础配置
     */
//    public static Retrofit getDefault() {
//        //配置OkHttp
//        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        builder.connectTimeout(20, TimeUnit.SECONDS);//链接超时20s
//        builder.readTimeout(20,TimeUnit.SECONDS);//写入缓存超时20s
//        builder.writeTimeout(20,TimeUnit.SECONDS);//读取缓存超时20s
//        builder.retryOnConnectionFailure(true);//错误重连
//        OkHttpClient client = builder.build();
//
//        mRetrofit = new Retrofit.Builder().baseUrl(ContactNet.BASEURL)
//                .addConverterFactory(SoapConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .client(client)
//                .build();
//        return mRetrofit;
//    }

    public static Retrofit getDefault_Retrofit_Http() {
        //配置OkHttp
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(20, TimeUnit.SECONDS);//链接超时20s
        builder.readTimeout(20,TimeUnit.SECONDS);//写入缓存超时20s
        builder.writeTimeout(20,TimeUnit.SECONDS);//读取缓存超时20s
        builder.retryOnConnectionFailure(true);//错误重连
        OkHttpClient client = builder.build();

        mRetrofit = new Retrofit.Builder().baseUrl(ContactNet.NEW_URL)
                .addConverterFactory(MyXmlConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        return mRetrofit;
    }

    public static Retrofit getDefault_Retrofit_Gson() {
        //配置OkHttp
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(20, TimeUnit.SECONDS);//链接超时20s
        builder.readTimeout(20,TimeUnit.SECONDS);//写入缓存超时20s
        builder.writeTimeout(20,TimeUnit.SECONDS);//读取缓存超时20s
        builder.retryOnConnectionFailure(true);//错误重连
        OkHttpClient client = builder.build();

        mRetrofit = new Retrofit.Builder().baseUrl(ContactNet.NEW_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        return mRetrofit;
    }

    public static Retrofit getRetrofit() {
        return mRetrofit;
    }

    /**
     * 获得retrofit接口实例
     *
     * @param reqServer
     * @param <T>
     * @return
     */
    protected <T> T createRequst(Class<T> reqServer) {
        return mRetrofit.create(reqServer);
    }
}
